package no.rmz.chez.tournament;
import no.rmz.chez.players.ChessPlayer;

public class PlayerPair implements Comparable<PlayerPair> {
    final ChessPlayer p1;
    final ChessPlayer p2;


    private static int comparePlayers(ChessPlayer p1, ChessPlayer p2) {
        if (p1 == p2) {
            return 0;
        } else {
            final int r =
                    Integer.signum(p1.hashCode() - p2.hashCode());
            if (r == 0) {
                throw new IllegalStateException("Two different chessplayers hash to the same value: " + p1 + ", " + p2);
            }
            return r;
        }
    }

    public PlayerPair(final ChessPlayer p1, final ChessPlayer p2) {
        super();
        assert( p1 != null);
        assert( p1 != null);
        int comp = Integer.signum(p1.hashCode() - p2.hashCode());
        assert (comp != 0);
        if (comp <= 0) {
            this.p1 = p1;
            this.p2 = p2;
        } else {
            this.p1 = p2;
            this.p2 = p1; 
        }
    }



    public final ChessPlayer getP1() {
        return p1;
    }

    public final ChessPlayer getP2() {
        return p2;
    }

    @Override
    public int compareTo(PlayerPair o) {
        int r =  comparePlayers(p1, o.p1);
        if (r == 0) {
            return 0;
        } else  {
            return comparePlayers(p2, o.p2);
        }
    }
}
